<?php

/**
 * Framework_Site_Common
 *
 * @author      Joe Stump <joe@joestump.net>
 * @copyright   (c) 2006 Joseph C. Stump. All rights reserved.
 * @license http://www.opensource.org/licenses/bsd-license.php
 * @package Framework
 * @filesource
 */



/**
 * Framework_Site_Common
 *
 * @author Joe Stump <joe@joestump.net>
 * @package Framework
 */
abstract class Framework_Site_Common //extends Framework_Object
{
	/**
	 * $name
	 *
	 * @access public
	 * @var string $name Name of site, used for paths
	 */
	public $name;

	/**
	 * $template
	 *
	 * @access public
	 * @var string $template Name of template(skin) to use
	 */
	public $template = 'Default';

	/**
	 * $options
	 *
	 * @access protected
	 * @var array $options An array to hold various options
	 */
	protected $options = array();

	/**
	 * $config
	 *
	 * @access  public
	 * @var     object Instance of SimpleXMLElement of site config
	 */
	public $config = null;

	/**
	 * __construct
	 *
	 * @access  public
	 * @return  void
	 */
	public function __construct()
	{
		if (!isset($this->name)) {
			throw new Framework_Exception('Site name not defined');
		}
		if (file_exists($this->getPath().'/Config.php')) {
			$this->config = include_once($this->getPath().'/Config.php');
		} else {
			throw new Framework_Exception("Could not load config: ".$this->getPath().'/config.xml');
		}

		$settings = $this->config('Framework', 'ini_set');
		foreach ($settings as $key => $val) {
			ini_set($key, $val);
		}

		// parent::__construct();
	}

	/**
	 *
	 * Safely gets a configuration group array or element value.
	 *
	 * @param string $group The name of the group.
	 *
	 * @param string $elem The name of the element in the group.
	 *
	 * @return mixed The value of the configuration group or element.
	 *
	 */
	public function &config($group, $elem = null)
	{
		// are we looking for a group or an element?
		if (is_null($elem)) {

			// looking for a group. if no default passed, set up an
			// empty array.
			$default = array();
			

			// find the requested group.
			if (empty($this->config[$group])) {
				return $default;
			} else {
				return $this->config[$group];
			}

		} else {

			// looking for an element. if no default passed, set up a
			// null.
			$default = null;
			

			// find the requested group and element.
			if (! isset($this->config[$group][$elem])) {
				return $default;
			} else {
				return $this->config[$group][$elem];
			}
		}
	}

	/**
	 * prepare
	 *
	 * @access public
	 * @return mixed True on success, PEAR_Error on failure
	 * @abstract
	 */
	abstract public function prepare();

	/**
	 * stop
	 *
	 * @access public
	 * @return mixed PEAR_Error on failure, true on success
	 */
	public function stop()
	{
		return true;
	}



	/**
	 * __set
	 *
	 * @access public
	 * @param string $var Variable to set in $options
	 * @param mixed $val Value of $var
	 * @return void
	 */
	public function __set($var,$val)
	{
		$this->options[$var] = $val;
	}

	/**
	 * __get
	 *
	 * @access public
	 * @param string $var Variable to get from $options
	 * @return void
	 */
	public function __get($var)
	{
		if (!isset($this->options[$var])) {
			$this->options[$var] = null;
		}

		return $this->options[$var];
	}

	/**
	 * getPath
	 *
	 * @access public
	 * @return string The file system path to the templates
	 */
	public function getPath()
	{
		return FRAMEWORK_BASE_PATH.'/Framework/Site/'.$this->name;
	}
	
	public function selfURL() {
		$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
		$protocol = $this->strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
		$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
		return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
	}
	private function strleft($s1, $s2) {
		return substr($s1, 0, strpos($s1, $s2));
	}

	public function getFilePath()
	{
		$path_parts = pathinfo($_SERVER['SCRIPT_NAME']);

		if($path_parts['dirname'] == '\\') {
			$path_parts['dirname'] = '';
		}
   
		return trim($_SERVER['DOCUMENT_ROOT'].$path_parts['dirname'], '/');
	}




}

?>
